<script setup>
import { knowledgeDelServe, knowledgeGetServe, knowledgeShowServe } from '@/api/knowledge'
import { Delete, Upload, Search, Edit } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores'
import KnowledgeUploadDrawer from './components/KnowledgeUploadDrawer.vue'
import { ref } from 'vue'

//
const {
  user: { name }
} = useUserStore()
//参数
const search = ref({ pagenum: 1, pagesize: 7 })
const total = ref(0)
const filetitle = ref('')
const contributor = ref('')
const getFileListLoading = ref(false)

const pageChange = (page) => {
  search.value.pagenum = page
  getFileList()
}
const handleSizeChange = (size) => {
  search.value.pagesize = size
  search.value.pagenum = 1
  getFileList()
}

//获取文件列表
const fileList = ref([])
const getFileList = async (obj) => {
  getFileListLoading.value = true
  obj = { ...obj, ...search.value }
  const res = await knowledgeGetServe(obj)
  fileList.value = res.record
  total.value = res.total
  getFileListLoading.value = false
  ElMessage.success({ message: '获取成功', offset: 70 })
}
getFileList()

//查询
const onSearch = () => {
  let obj = ref({})
  if (filetitle.value) {
    obj.value = { filetitle: filetitle.value }
  }
  if (contributor.value) {
    obj.value = { ...obj.value, contributor: contributor.value }
  }
  search.value.pagenum = 1
  getFileList(obj.value)
}
const onReset = () => {
  filetitle.value = ''
  contributor.value = ''
  getFileList()
}

//操作
const onShowFile = async (id) => {
  const res = await knowledgeShowServe(id)
  const pdfUrl = URL.createObjectURL(res)
  const newWindow = window.open(pdfUrl, '_blank')
  if (!newWindow) {
    ElMessage.error({ message: '无法打开窗口，请检查浏览器设置', offset: 70 })
  }
}
const onRemove = async (id) => {
  await ElMessageBox.confirm('确认删除该文件吗？', '温馨提醒', {
    type: 'warning',
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
  await knowledgeDelServe(id)
  getFileList()
  ElMessage.success({ message: '删除成功', offset: 70 })
}
//修改
const onEdit = (row) => {
  knowledgeUploadRef.value.onOpenUpload(row)
}
//上传
const knowledgeUploadRef = ref()
const onOpenUpload = () => {
  knowledgeUploadRef.value.onOpenUpload()
}
//刷新
const refresh = (flag) => {
  if (!flag) {
    filetitle.value = ''
    contributor.value = ''
    search.value.pagenum = Math.ceil((total.value + 1) / search.value.pagesize)
  }
  getFileList()
}
</script>

<template>
  <div style="display: flex; justify-content: space-between">
    <el-form inline>
      <el-form-item style="width: 200px; margin-right: 5px">
        <el-input v-model="filetitle" placeholder="输入文件标题模糊查询"></el-input>
      </el-form-item>
      <el-form-item style="width: 200px">
        <el-input v-model="contributor" placeholder="输入作者模糊查询"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button type="primary" plain @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-right: 50px">
      <el-button type="primary" @click="onOpenUpload" :icon="Upload"> 上传文件 </el-button>
    </div>
  </div>
  <!-- 文件列表 -->
  <el-table :data="fileList" v-loading="getFileListLoading">
    <el-table-column label="序号" type="index" width="123"> </el-table-column>
    <el-table-column label="标题" prop="filetitle"> </el-table-column>
    <el-table-column label="作者" prop="contributor"> </el-table-column>
    <el-table-column label="描述" prop="" width="400">
      <template #default="{ row: { filedescription } }">
        <el-popover
          ref="popover"
          placement="right"
          title="文件描述"
          :width="300"
          trigger="hover"
          :content="filedescription"
        >
          <template #reference>
            <el-button class="m-2">查看描述</el-button>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="250">
      <template #default="{ row }">
        <el-button size="default" type="primary" @click="onShowFile(row.id)" :icon="Search">
          查看文件
        </el-button>
        <el-button
          :disabled="row.contributor !== name"
          size="default"
          type="warning"
          plain
          @click="onEdit(row)"
          :icon="Edit"
        >
        </el-button>
        <el-button size="default" type="danger" plain @click="onRemove(row.id)" :icon="Delete">
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- ----分页---- -->
  <el-pagination
    v-if="fileList[0]"
    v-model:current-page="search.pagenum"
    v-model:page-size="search.pagesize"
    :background="true"
    :page-sizes="[7, 9, 12, 15]"
    layout="total, prev, pager, next, sizes"
    :total="total"
    style="margin-top: 20px; justify-content: flex-end"
    @current-change="pageChange"
    @size-change="handleSizeChange"
  />
  <!-- 上传 -->
  <KnowledgeUploadDrawer ref="knowledgeUploadRef" @onShowFile="onShowFile" @refresh="refresh" />
</template>

<style lang="" scoped></style>
